package de.bastian.hinterleitner;

import java.util.Calendar;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.RemoteViews;
import de.bastian.hinterleitner.database.DataHelper;
import de.bastian.hinterleitner.tools.Entry;
import de.bastian.hinterleitner.tools.Helper;
import de.bastian.hinterleitner.tools.UpdateWidgetService;
import de.hijacksoft.wyc.R;

public class Widget extends AppWidgetProvider {

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {

		Log.d("WIDGET", "updating ...");

		for (int widgetId : appWidgetIds) {
			RemoteViews remoteViews = new RemoteViews(context
					.getApplicationContext().getPackageName(),
					R.layout.widget_layout);
			Log.d("SERVICE", "id:" + widgetId);

			configureRemoteViews(context, remoteViews);
			
			// Register a start onClickListener
			Intent clickIntent = new Intent(context.getApplicationContext(),
					Main.class);
			clickIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

			PendingIntent pendingIntent2 = PendingIntent.getActivity(context,
					widgetId, clickIntent, Intent.FLAG_ACTIVITY_CLEAR_TOP);

			remoteViews.setOnClickPendingIntent(R.id.widget_text,
					pendingIntent2);

			// Register an add onClickListener
			Intent addIntent = new Intent(context.getApplicationContext(),
					NewEntry.class);
			addIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

			PendingIntent pendingIntent3 = PendingIntent.getActivity(context,
					widgetId, addIntent, Intent.FLAG_ACTIVITY_CLEAR_TASK);

			remoteViews
					.setOnClickPendingIntent(R.id.widget_add, pendingIntent3);

			appWidgetManager.updateAppWidget(widgetId, remoteViews);
		}
	}

	public static RemoteViews configureRemoteViews(Context context,
			RemoteViews remoteViews) {
		// update total and budget:

		SharedPreferences sp = context.getSharedPreferences("values",
				android.content.Context.MODE_PRIVATE);
		double budget = sp.getFloat("budget", 200);
		double total = Helper.getThisMonthsTotal(context);

		String totalTxt = (context.getString(R.string.totalmonth) + "\n"
				+ roundToDecimals(2, total) + " " + context
				.getString(R.string.currency));

		String budgetTxt;

		if (total < -budget) {
			budgetTxt = context.getString(R.string.overbudget).replace(
					"$1",
					roundToDecimals(2, -budget - total) + " "
							+ context.getString(R.string.currency));
		} else {
			budgetTxt = context.getString(R.string.budget) + ": "
					+ roundToDecimals(2, budget + total) + "/" +  roundToDecimals(2,budget)
					+ " " + context.getString(R.string.currency);
		}

		remoteViews.setTextViewText(R.id.widget_text, totalTxt + "\n"
				+ budgetTxt);

		return remoteViews;
	}

	public static final double roundToDecimals(int decimals, double src) {
		return Math.round((src * Math.pow(10, decimals))) / Math.pow(10, decimals);
	}

}